//
//  ChannelUnitModel.m
//  ebook
//
//  Created by 吴超飞 on 2016/11/26.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "ChannelUnitModel.h"
#import <objc/runtime.h>

@implementation ChannelUnitModel

-(id)initWithDict:(NSDictionary *)dict{
   
    self = [super init];
    if (self) {
        NSArray *keys = [dict allKeys];
        self.ID = [keys containsObject:@"ID"]?dict[@"ID"]:@"";
        self.Name = [keys containsObject:@"Name"]?dict[@"Name"]:@"";
        self.IsAdd = [keys containsObject:@"IsAdd"]?[dict[@"IsAdd"] boolValue]:NO;
        self.IsTop = [keys containsObject:@"IsTop"]?[dict[@"IsTop"] boolValue]:NO;
    }
    return self;
}

-(NSString *)description{
    
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList(self.class, &count);
    NSMutableString *str = [NSMutableString string];
    for (int i = 0; i < count; ++i) {
        Ivar ivar = ivars[i];
        const char *name = ivar_getName(ivar);
        NSString *proName = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:proName];
        
        [str appendFormat:@"<%@ : %@> \n", proName, value];
    }
    free(ivars);
    return str;
}

-(NSMutableDictionary *)dictWithModel//:(ChannelUnitModel*)model
{
    
    NSMutableDictionary *userDic = [NSMutableDictionary dictionary];
    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList([ChannelUnitModel class], &count);
    
    for (int i = 0; i < count; i++) {
        
        const char *name = property_getName(properties[i]);
        
        NSString *propertyName = [NSString stringWithUTF8String:name];
        
        id propertyValue = [self valueForKey:propertyName];
        
        if (propertyValue) {
            
            [userDic setObject:propertyValue forKey:propertyName];
            
        }
        
    }
    
    free(properties);
    return userDic;
}

@end
